<script>
import { mapActions } from 'vuex'
import { isH5 } from './env'
export default {
  onLaunch: function (args) {
    if (isH5()) {
      document.title = uni.rawTitle
    }
    // this.checkIsPad()
    this.setNavBarInfo()
    this.fetchAppVersion()
    this.checkUpdate()
  },
  onShow: function () {
    console.log('App Show')
  },
  onHide: function () {
    console.log('App Hide')
  },
  onPageNotFound: function (args) {
    uni.redirectTo({
      url: '/pages/index/index'
    })
  },
  methods: {
    ...mapActions(['setNavBarInfo', 'fetchAppVersion']),
    checkIsPad () {
      const { deviceType } = uni.getSystemInfoSync()
      if (deviceType === 'pad') {
        uni.showModal({
          title: '提示',
          content: '为保证服务体验，请使用手机体验小程序',
          showCancel: false
        })
      }
    },
    checkUpdate () {
      try {
        const updateManager = uni.getUpdateManager()
        updateManager.onCheckForUpdate((res) => {
          console.log(res)
          if (res.hasUpdate) {
            updateManager.onUpdateReady(() => {
              uni.showModal({
                title: '更新提示',
                content: '新版本已经准备好，将自动重启应用',
                showCancel: false,
                confirmText: '我知道了',
                success: function (res) {
                  if (res.confirm) {
                    updateManager.applyUpdate()
                  }
                }
              })
            })
            updateManager.onUpdateFailed(() => {
              uni.showModal({
                title: '已经有新版本',
                content: '新版本已经上线，请删除当前小程序，重新搜索打开'
              })
            })
          }
        })
      } catch (e) {}
    }
  }
}
</script>

<style lang="less">
/*每个页面公共css */
@import './css/index.less';
/* #ifdef H5 */
@import './css/component.less';
/* #endif */
</style>
